/**
 * Textbooklet
 * this class exists only for the need to have a singleton to access the text values and appearances
 * @author kzm
 * @version 1.0 
 */
package com.mediacatalyst.core
{

	public class Textbooklet
	{		
		private static var _data:IdentifiableXML;

		public function Textbooklet(){
			if(_data == null)
				_data = new IdentifiableXML();
		}
		
		public function setNewXML(key:String,value:XML):void{
			_data.add(key,value);
		}
		
		public function getCopy(_xml:String,id:String): String{
			var xml:XML = _data.retrieve(_xml);
			if(xml!=null)
				return xml.*.( hasOwnProperty( "@key" ) && @key == id )[0];
			else
				return "";
		}
		
		public function getAttribute(_xml:String,id:String,attr:String): String {
			var xml:XML = _data.retrieve(_xml);
			if(xml!=null){
				return xml.*.( hasOwnProperty( "@key" ) && @key == id ).attribute(attr);
			} else
				return "";
		}
		
		public function size(xml:String):uint{
			try{
				return _data.retrieve(xml).length();
			} finally {
				return 0;
			}
			return 0;
		}
		
		public function getXML( key : String ) : XML
		{
			return _data.getXML(key);
		}	
		
		public function sizeByKey(xml:String,key:String):uint{
			var xm:XML = _data.retrieve(xml);
			if(xm==null)
				return 0;
			var list:XMLList = xm.*.( new RegExp('^'+key+'.*','g')['test'](@key) );
			return list.length();
		}
	
	}	
}

import flash.utils.Dictionary;

class IdentifiableXML{
	private var _storage:Dictionary;
	
	public function IdentifiableXML(){
		_storage = new Dictionary( false );
	}
	
	public function add(key:String,value:XML):Boolean{
		key = key.toLowerCase();
		if(inUse(key) || exists(value))
			return false;
		else 
			_storage[key] = value;
			return true;
	}
	
	public function retrieve(key:String):XML{
		return _storage[key.toLowerCase()];
	}
	
	public function getXML(key:String):XML{
		key = key.toLowerCase();
		for(var storage_key:String in _storage){
			if(storage_key == key)
				return _storage[key];
		}
		return null;
	}
	
	public function exists(xml:XML):Boolean{
		for each(var storage_xml:XML in _storage){
			if(storage_xml == xml)
				return true;
		}
		return false;
	}
	
	public function inUse(key:String):Boolean{
		key = key.toLowerCase();
		for(var storage_key:String in _storage){
			if(storage_key == key)
				return true;
		}
		return false;
	}
}